home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / core.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-03  |  13.8 KB  |  570 lines

  1. /* Work with core dump and executable files, for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"  /* required by inferior.h */
  24. #include "inferior.h"
  25.  
  26. #ifdef USG
  27. #include <sys/types.h>
  28. #include <fcntl.h>
  29. #endif
  30.  
  31. #ifdef COFF_ENCAPSULATE
  32. #include "a.out.encap.h"
  33. #else
  34. #include <a.out.h>
  35. #endif
  36. #ifndef N_MAGIC
  37. #ifdef COFF_FORMAT
  38. #define N_MAGIC(exec) ((exec).magic)
  39. #else
  40. #define N_MAGIC(exec) ((exec).a_magic)
  41. #endif
  42. #endif
  43. #include <signal.h>
  44. #include <sys/param.h>
  45. #include <sys/dir.h>
  46. #include <sys/file.h>
  47. #include <sys/stat.h>
  48.  
  49. #ifdef UMAX_CORE
  50. #include <sys/ptrace.h>
  51. #else
  52. #include <sys/user.h>
  53. #endif
  54.  
  55. #ifndef N_TXTADDR
  56. #define N_TXTADDR(hdr) 0
  57. #endif /* no N_TXTADDR */
  58.  
  59. #ifndef N_DATADDR
  60. #define N_DATADDR(hdr) hdr.a_text
  61. #endif /* no N_DATADDR */
  62.  
  63. #ifndef COFF_FORMAT
  64. #ifndef AOUTHDR
  65. #define AOUTHDR        struct exec
  66. #endif
  67. #endif
  68.  
  69. extern char *sys_siglist[];
  70.  
  71. extern core_file_command (), exec_file_command ();
  72.  
  73. /* Hook for `exec_file_command' command to call.  */
  74.  
  75. void (*exec_file_display_hook) ();
  76.    
  77. /* File names of core file and executable file.  */
  78.  
  79. char *corefile;
  80. char *execfile;
  81.  
  82. /* Descriptors on which core file and executable file are open.
  83.    Note that the execchan is closed when an inferior is created
  84.    and reopened if the inferior dies or is killed.  */
  85.  
  86. int corechan;
  87. int execchan;
  88.  
  89. /* Last modification time of executable file.
  90.    Also used in source.c to compare against mtime of a source file.  */
  91.  
  92. int exec_mtime;
  93.  
  94. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  95.  
  96. CORE_ADDR data_start;
  97. CORE_ADDR data_end;
  98. CORE_ADDR stack_start;
  99. CORE_ADDR stack_end;
  100.  
  101. #if defined (REG_STACK_SEGMENT)
  102. /* Start and end of the register stack segment.  */
  103. CORE_ADDR reg_stack_start;
  104. CORE_ADDR reg_stack_end;
  105. #endif /* REG_STACK_SEGMENT */
  106.  
  107. /* Virtual addresses of bounds of two areas of memory in the exec file.
  108.    Note that the data area in the exec file is used only when there is no core file.  */
  109.  
  110. CORE_ADDR text_start;
  111. CORE_ADDR text_end;
  112.  
  113. CORE_ADDR exec_data_start;
  114. CORE_ADDR exec_data_end;
  115.  
  116. /* Offset within executable file of start of text area data.  */
  117.  
  118. int text_offset;
  119.  
  120. /* Offset within executable file of start of data area data.  */
  121.  
  122. int exec_data_offset;
  123.  
  124. /* Offset within core file of start of data area data.  */
  125.  
  126. int data_offset;
  127.  
  128. /* Offset within core file of start of stack area data.  */
  129.  
  130. int stack_offset;
  131.   
  132. #ifdef COFF_FORMAT
  133. /* various coff data structures */
  134.  
  135. FILHDR file_hdr;
  136. SCNHDR text_hdr;
  137. SCNHDR data_hdr;
  138.  
  139. #endif /* not COFF_FORMAT */
  140.  
  141. /* a.out header saved in core file.  */
  142.   
  143. AOUTHDR core_aouthdr;
  144.  
  145. /* a.out header of exec file.  */
  146.  
  147. AOUTHDR exec_aouthdr;
  148.  
  149. void validate_files ();
  150. unsigned int register_addr ();
  151.  
  152. /* Call this to specify the hook for exec_file_command to call back.
  153.    This is called from the x-window display code.  */
  154.  
  155. void
  156. specify_exec_file_hook (hook)
  157.      void (*hook) ();
  158. {
  159.   exec_file_display_hook = hook;
  160. }
  161.  
  162. /* The exec file must be closed before running an inferior.
  163.    If it is needed again after the inferior dies, it must
  164.    be reopened.  */
  165.  
  166. void
  167. close_exec_file ()
  168. {
  169.   if (execchan >= 0)
  170.     close (execchan);
  171.   execchan = -1;
  172. }
  173.  
  174. void
  175. reopen_exec_file ()
  176. {
  177.   if (execchan < 0 && execfile != 0)
  178.     {
  179.       char *filename = concat (execfile, "", "");
  180.       exec_file_command (filename, 0);
  181.       free (filename);
  182.     }
  183. }
  184.  
  185. /* If we have both a core file and an exec file,
  186.    print a warning if they don't go together.
  187.    This should really check that the core file came
  188.    from that exec file, but I don't know how to do it.  */
  189.  
  190. void
  191. validate_files ()
  192. {
  193.   if (execfile != 0 && corefile != 0)
  194.     {
  195.       struct stat st_core;
  196.  
  197.       if (fstat (corechan, &st_core) < 0)
  198.     /* It might be a good idea to print an error message.
  199.        On the other hand, if the user tries to *do* anything with
  200.        the core file, (s)he'll find out soon enough.  */
  201.     return;
  202.  
  203.       if (N_MAGIC (core_aouthdr) != 0
  204. #if (defined(sprite) && !defined(mips))
  205.      /*
  206.       * The core_aouthdr in Sprite as generated by gcore is 
  207.       * only a guess at the real exec header. The only thing that
  208.       * should always be right is the text size.
  209.       */
  210.       && (core_aouthdr.a_text != exec_aouthdr.a_text))
  211. #else
  212.           && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
  213. #endif
  214.     printf ("Warning: core file does not match specified executable file.\n");
  215.       else if (exec_mtime > st_core.st_mtime)
  216.     printf ("Warning: exec file is newer than core file.\n");
  217.     }
  218. }
  219.  
  220. /* Return the name of the executable file as a string.
  221.    ERR nonzero means get error if there is none specified;
  222.    otherwise return 0 in that case.  */
  223.  
  224. char *
  225. get_exec_file (err)
  226.      int err;
  227. {
  228.   if (err && execfile == 0)
  229.     error ("No executable file specified.\n\
  230. Use the \"exec-file\" and \"symbol-file\" commands.");
  231.   return execfile;
  232. }
  233.  
  234. int
  235. have_core_file_p ()
  236. {
  237.   return corefile != 0;
  238. }
  239.  
  240. static void
  241. files_info ()
  242. {
  243.   char *symfile;
  244.   extern char *get_sym_file ();
  245.  
  246.   if (execfile)
  247.     printf ("Executable file \"%s\".\n", execfile);
  248.   else
  249.     printf ("No executable file\n");
  250.   if (corefile == 0)
  251.     printf ("No core dump file\n");
  252.   else
  253.     printf ("Core dump file \"%s\".\n", corefile);
  254.  
  255.   if (have_inferior_p ())
  256.     printf ("Using the running image of the program, rather than these files.\n");
  257.  
  258.   symfile = get_sym_file ();
  259.   if (symfile != 0)
  260.     printf ("Symbols from \"%s\".\n", symfile);
  261.  
  262. #ifdef FILES_INFO_HOOK
  263.   if (FILES_INFO_HOOK ())
  264.     return;
  265. #endif
  266.  
  267.   if (! have_inferior_p ())
  268.     {
  269.       if (execfile)
  270.     {
  271.       printf ("Text segment in executable from 0x%x to 0x%x.\n",
  272.           text_start, text_end);
  273.       printf ("Data segment in executable from 0x%x to 0x%x.\n",
  274.           exec_data_start, exec_data_end);
  275.       if (corefile)
  276.         printf ("(But since we have a core file, we're using...)\n");
  277.     }
  278.       if (corefile)
  279.     {
  280.       printf ("Data segment in core file from 0x%x to 0x%x.\n",
  281.           data_start, data_end);
  282.       printf ("Stack segment in core file from 0x%x to 0x%x.\n",
  283.           stack_start, stack_end);
  284.     }
  285.     }
  286. }
  287.  
  288. /* Read "memory data" from core file and/or executable file.
  289.    Returns zero if successful, 1 if xfer_core_file failed, errno value if
  290.    ptrace failed. */
  291.  
  292. int
  293. read_memory (memaddr, myaddr, len)
  294.      CORE_ADDR memaddr;
  295.      char *myaddr;
  296.      int len;
  297. {
  298.   if (len == 0)
  299.     return 0;
  300.  
  301.   if (have_inferior_p ())
  302.     {
  303.       if (remote_debugging)
  304.     return remote_read_inferior_memory (memaddr, myaddr, len);
  305.       else
  306.     return read_inferior_memory (memaddr, myaddr, len);
  307.     }
  308.   else
  309.       return xfer_core_file (memaddr, myaddr, len);
  310. }
  311.  
  312. /* Write LEN bytes of data starting at address MYADDR
  313.    into debugged program memory at address MEMADDR.
  314.    Returns zero if successful, or an errno value if ptrace failed.  */
  315.  
  316. int
  317. write_memory (memaddr, myaddr, len)
  318.      CORE_ADDR memaddr;
  319.      char *myaddr;
  320.      int len;
  321. {
  322.   if (have_inferior_p ())
  323.     {
  324.       if (remote_debugging)
  325.     return remote_write_inferior_memory (memaddr, myaddr, len);
  326.       else
  327.     return write_inferior_memory (memaddr, myaddr, len);
  328.     }
  329.   else
  330.     error ("Can write memory only when program being debugged is running.");
  331. }
  332.  
  333. #ifndef XFER_CORE_FILE
  334. /* Read from the program's memory (except for inferior processes).
  335.    This function is misnamed, since it only reads, never writes; and
  336.    since it will use the core file and/or executable file as necessary.
  337.  
  338.    It should be extended to write as well as read, FIXME, for patching files.
  339.  
  340.    Return 0 if address could be read, 1 if not. */
  341.  
  342. int
  343. xfer_core_file (memaddr, myaddr, len)
  344.      CORE_ADDR memaddr;
  345.      char *myaddr;
  346.      int len;
  347. {
  348.   register int i;
  349.   register int val;
  350.   int xferchan;
  351.   char **xferfile;
  352.   int fileptr;
  353.   int returnval = 0;
  354.  
  355.   while (len > 0)
  356.     {
  357.       xferfile = 0;
  358.       xferchan = 0;
  359.  
  360.       /* Determine which file the next bunch of addresses reside in,
  361.      and where in the file.  Set the file's read/write pointer
  362.      to point at the proper place for the desired address
  363.      and set xferfile and xferchan for the correct file.
  364.  
  365.      If desired address is nonexistent, leave them zero.
  366.  
  367.      i is set to the number of bytes that can be handled
  368.      along with the next address.
  369.  
  370.      We put the most likely tests first for efficiency.  */
  371.  
  372.       /* Note that if there is no core file
  373.      data_start and data_end are equal.  */
  374.       if (memaddr >= data_start && memaddr < data_end)
  375.     {
  376.       i = min (len, data_end - memaddr);
  377.       fileptr = memaddr - data_start + data_offset;
  378.       xferfile = &corefile;
  379.       xferchan = corechan;
  380.     }
  381.       /* Note that if there is no core file
  382.      stack_start and stack_end are equal.  */
  383.       else if (memaddr >= stack_start && memaddr < stack_end)
  384.     {
  385.       i = min (len, stack_end - memaddr);
  386.       fileptr = memaddr - stack_start + stack_offset;
  387.       xferfile = &corefile;
  388.       xferchan = corechan;
  389.     }
  390. #ifdef REG_STACK_SEGMENT
  391.       /* Pyramids have an extra segment in the virtual address space
  392.          for the (control) stack of register-window frames */
  393.       else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
  394.     {
  395.       i = min (len, reg_stack_end - memaddr);
  396.       fileptr = memaddr - reg_stack_start + reg_stack_offset;
  397.       xferfile = &corefile;
  398.       xferchan = corechan;
  399.     }
  400. #endif /* REG_STACK_SEGMENT */
  401.  
  402.       else if (corechan < 0
  403.            && memaddr >= exec_data_start && memaddr < exec_data_end)
  404.     {
  405.       i = min (len, exec_data_end - memaddr);
  406.       fileptr = memaddr - exec_data_start + exec_data_offset;
  407.       xferfile = &execfile;
  408.       xferchan = execchan;
  409.     }
  410.       else if (memaddr >= text_start && memaddr < text_end)
  411.     {
  412.       i = min (len, text_end - memaddr);
  413.       fileptr = memaddr - text_start + text_offset;
  414.       xferfile = &execfile;
  415.       xferchan = execchan;
  416.     }
  417.       else if (memaddr < text_start)
  418.     {
  419.       i = min (len, text_start - memaddr);
  420.     }
  421.       else if (memaddr >= text_end
  422.            && memaddr < (corechan >= 0? data_start : exec_data_start))
  423.     {
  424.       i = min (len, data_start - memaddr);
  425.     }
  426.       else if (corechan >= 0
  427.            && memaddr >= data_end && memaddr < stack_start)
  428.     {
  429.       i = min (len, stack_start - memaddr);
  430.     }
  431.       else if (corechan < 0 && memaddr >= exec_data_end)
  432.     {
  433.       /* Since there is nothing at higher addresses than data
  434.          (without a core file or an inferior, there is no
  435.          stack, set i to do the rest of the operation now.  */
  436.       i = len;
  437.     }
  438. #ifdef REG_STACK_SEGMENT
  439.       else if (memaddr >= reg_stack_end && reg_stack_end != 0)
  440.     {
  441.       i = min (len, reg_stack_start - memaddr);
  442.     }
  443.       else if (memaddr >= stack_end && memaddr < reg_stack_start)
  444. #else /* no REG_STACK_SEGMENT.  */
  445.       else if (memaddr >= stack_end && stack_end != 0)
  446. #endif /* no REG_STACK_SEGMENT.  */
  447.     {
  448.       /* Since there is nothing at higher addresses than
  449.          the stack, set i to do the rest of the operation now.  */
  450.       i = len;
  451.     }
  452.       else
  453.     {
  454.       /* Address did not classify into one of the known ranges.
  455.          This shouldn't happen; we catch the endpoints.  */
  456.       fatal ("Internal: Bad case logic in xfer_core_file.");
  457.     }
  458.  
  459.       /* Now we know which file to use.
  460.      Set up its pointer and transfer the data.  */
  461.       if (xferfile)
  462.     {
  463.       if (*xferfile == 0)
  464.         if (xferfile == &execfile)
  465.           error ("No program file to examine.");
  466.         else
  467.           error ("No core dump file or running program to examine.");
  468.       val = lseek (xferchan, fileptr, 0);
  469.       if (val < 0)
  470.         perror_with_name (*xferfile);
  471.       val = myread (xferchan, myaddr, i);
  472.       if (val < 0)
  473.         perror_with_name (*xferfile);
  474.     }
  475.       /* If this address is for nonexistent memory,
  476.      read zeros if reading, or do nothing if writing.
  477.      Actually, we never right.  */
  478.       else
  479.     {
  480.       bzero (myaddr, i);
  481.       returnval = 1;
  482.     }
  483.  
  484.       memaddr += i;
  485.       myaddr += i;
  486.       len -= i;
  487.     }
  488.   return returnval;
  489. }
  490. #endif /* XFER_CORE_FILE */
  491.  
  492. /* My replacement for the read system call.
  493.    Used like `read' but keeps going if `read' returns too soon.  */
  494.  
  495. int
  496. myread (desc, addr, len)
  497.      int desc;
  498.      char *addr;
  499.      int len;
  500. {
  501.   register int val;
  502.   int orglen = len;
  503.  
  504.   while (len > 0)
  505.     {
  506.       val = read (desc, addr, len);
  507.       if (val < 0)
  508.     return val;
  509.       if (val == 0)
  510.     return orglen - len;
  511.       len -= val;
  512.       addr += val;
  513.     }
  514.   return orglen;
  515. }
  516.  
  517. #ifdef REGISTER_U_ADDR
  518.  
  519. /* Return the address in the core dump or inferior of register REGNO.
  520.    BLOCKEND is the address of the end of the user structure.  */
  521.  
  522. unsigned int
  523. register_addr (regno, blockend)
  524.      int regno;
  525.      int blockend;
  526. {
  527.   int addr;
  528.  
  529.   if (regno < 0 || regno >= NUM_REGS)
  530.     error ("Invalid register number %d.", regno);
  531.  
  532.   REGISTER_U_ADDR (addr, blockend, regno);
  533.  
  534.   return addr;
  535. }
  536.  
  537. #endif /* REGISTER_U_ADDR */
  538.  
  539. void
  540. _initialize_core()
  541. {
  542.   corechan = -1;
  543.   execchan = -1;
  544.   corefile = 0;
  545.   execfile = 0;
  546.   exec_file_display_hook = 0;
  547.  
  548.   text_start = 0;
  549.   text_end = 0;
  550.   data_start = 0;
  551.   data_end = 0;
  552.   exec_data_start = 0;
  553.   exec_data_end = 0;
  554.   stack_start = STACK_END_ADDR;
  555.   stack_end = STACK_END_ADDR;
  556.  
  557. #if (defined(sprite) && !defined(mips))
  558.   add_com ("core-file", class_files, core_file_command,
  559.        "Use FILE as core dump for examining memory and registers.\n\
  560. No arg means have no core file.");
  561. #endif
  562.   add_com ("exec-file", class_files, exec_file_command,
  563.        "Use FILE as program for getting contents of pure memory.\n\
  564. If FILE cannot be found as specified, your execution directory path\n\
  565. is searched for a command of that name.\n\
  566. No arg means have no executable file.");
  567.   add_info ("files", files_info, "Names of files being debugged.");
  568. }
  569.  
  570.